package com.gluck.cases.core.block.impl;

import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gluck.cases.common.utils.dal.dao.CasesGroupMapper;
import com.gluck.cases.common.utils.enums.BizSerErrorEnum;
import com.gluck.cases.common.utils.utils.AssertUtil;
import com.gluck.cases.common.utils.utils.TimeUtils;
import com.gluck.cases.core.block.CasesApiBlock;
import com.gluck.cases.core.block.CasesGroupBlock;
import com.gluck.cases.core.block.CasesTestsBlock;
import com.gluck.cases.core.modal.assemble.CasesGroupAssemble;
import com.gluck.cases.core.modal.assemble.PageInfoAssemble;
import com.gluck.cases.core.modal.dto.CasesApiDTO;
import com.gluck.cases.core.modal.dto.CasesGroupDTO;
import com.gluck.cases.core.modal.dto.CasesTestsDTO;
import com.gluck.cases.core.modal.entity.CasesGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @ClassName CasesGroupBlockImpl
 * @Desc CasesGroupBlockImpl
 * @Author shiyongzhe
 * @Date 2022/5/28 - 10:09 下午
 **/
@Service
public class CasesGroupBlockImpl implements CasesGroupBlock {

    @Autowired
    private CasesGroupMapper casesGroupMapper;

    @Autowired
    private CasesTestsBlock casesTestsBlock;

    @Autowired
    private CasesApiBlock casesApiBlock;

    @Override
    public PageInfo<CasesGroupDTO> queryGroupsByCreateBy(String username, String name, Integer startPage, Integer endPage) {
        PageInfo<CasesGroup> pageInfo = new PageInfo<>(PageHelper.startPage(startPage, endPage).doSelectPage(new ISelect() {
            @Override
            public void doSelect() {
                casesGroupMapper.queryGroupsByCreateBy(username, name);
            }
        }));
        List<CasesGroupDTO> dtos = pageInfo.getList().stream().map(x -> {
            CasesGroupDTO dto = CasesGroupAssemble.CasesGroupConvertDTO(x);
            return dto;
        }).collect(Collectors.toList());
        PageInfoAssemble<CasesGroupDTO> assemble = new PageInfoAssemble<CasesGroupDTO>();
        PageInfo<CasesGroupDTO> result = assemble.covertDtoPageInfo(pageInfo, dtos);
        return result;
    }


    @Override
    public PageInfo<CasesTestsDTO> queryTestsByGroupId(Long id, Integer startPage, Integer endPage) {
        PageInfo<CasesTestsDTO> casesTests = casesTestsBlock.queryCasesTests(id, startPage, endPage);
        return casesTests;
    }

    @Override
    public boolean addGroup(CasesGroupDTO dto) {
        int insert = casesGroupMapper.insert(CasesGroupAssemble.CasesGroupDTOConvertCasesGroup(dto));
        return insert > 0;
    }

    @Override
    public boolean deleteGroupById(Long id) {
        int delete = casesGroupMapper.deleteById(id);
        return delete > 0;
    }

    @Override
    public boolean updateGroup(CasesGroupDTO dto) {
        int update = casesGroupMapper.updateGroupData(CasesGroupAssemble.CasesGroupDTOConvertCasesGroup(dto));
        return update > 0;
    }

    @Override
    public boolean associateCasesTestsByGroupId(Long gid, String username, List<Long> casesIds){
        casesIds.stream().forEach(
            casesId -> {
                CasesApiDTO dto = casesApiBlock.queryById(casesId);
                if (!Objects.isNull(dto)) {
                    CasesTestsDTO casesTests = new CasesTestsDTO();
                    casesTests.setCasesApi(dto.getId());
                    // todo 暂时用params 代替data_info 信息
                    casesTests.setDataInfo(dto.getParams());
                    casesTests.setCreatedAt(TimeUtils.format(LocalDateTime.now(), TimeUtils.DATETIME_FORMATTER));
                    casesTests.setUpdatedAt(TimeUtils.format(LocalDateTime.now(), TimeUtils.DATETIME_FORMATTER));
                    casesTests.setCreatedBy(username);
                    casesTests.setGroupId(gid);
                    AssertUtil.isFalse(casesTestsBlock.addCasesTest(casesTests), BizSerErrorEnum.CASES_TESTS_ADD_ERROR);
                }
            }
        );
        return true;
    };

}
